#!/bin/bash

# Script to reformat images to fit into 1024x768 max pixels and create a square cropped thumbnail image
# intended for use on the team website photo library.
#
# Takes a directory as an argument and modifies any .jpg or .JPG filename
#
# NOTE: This permanently changes the source images, it does not make copies (this is due to harddrive
#       space limitations on my system).

if [ -n "$1" ]
then
  DIRECTORY=$1
else
  DIRECTORY=$PWD
fi

rename -v 'y/\ /_/' *.jpg *.JPG

for file in `ls $DIRECTORY | grep "jpg\|.JPG"`
do
  echo "shrinking ${file}"
  convert $DIRECTORY/${file} -resize 1024x768 $DIRECTORY/${file}
  convert $DIRECTORY/${file} -gravity Center -crop 685x685+0+0 +repage -resize 180x180 $DIRECTORY/${file%.[jJ][pP][gG]}icon.jpg
done

exit 0
